package io.terminus.spzx.order.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.model.dto.h5.OrderInfoDto;
import io.terminus.spzx.model.entity.order.OrderInfo;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import io.terminus.spzx.model.vo.h5.TradeVo;
import io.terminus.spzx.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 【移动端】订单管理Controller
 * @Date 2024-12-08
 */
@RestController
@Tag(name = "订单管理接口")
@RequiredArgsConstructor
@RequestMapping(value = "/api/order/orderInfo")
public class OrderController {

    private final OrderService orderService;

    @GetMapping("auth/trade")
    @Operation(summary = "确认下单")
    public Result<TradeVo> trade() {
        TradeVo tradeVo = orderService.getTrade();

        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "提交订单")
    @PostMapping("auth/submitOrder")
    public Result<Long> submitOrder(@Parameter(name = "orderInfoDto", description = "请求参数DTO", required = true) @RequestBody OrderInfoDto orderInfoDto) {
        Long orderId = orderService.submitOrder(orderInfoDto);

        return Result.build(orderId, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单信息")
    @GetMapping("auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@Parameter(name = "orderId", description = "订单id", required = true) @PathVariable Long orderId) {
        OrderInfo orderInfo = orderService.getOrderInfo(orderId);

        return Result.build(orderInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "立即购买")
    @GetMapping("auth/buy/{skuId}")
    public Result<TradeVo> buy(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable Long skuId) {
        TradeVo tradeVo = orderService.buy(skuId);

        return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取订单分页列表")
    @GetMapping("auth/{page}/{limit}")
    public Result<PageInfo<OrderInfo>> list(
            @Parameter(name = "page", description = "当前页码", required = true) @PathVariable Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable Integer limit,
            @Parameter(name = "orderStatus", description = "订单状态") @RequestParam(required = false, defaultValue = StringUtils.EMPTY) Integer orderStatus) {
        PageInfo<OrderInfo> pageInfo = orderService.findUserPage(page, limit, orderStatus);

        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    // OpenFeign远程调用: 供支付模块查询订单使用
    @Operation(summary = "获取订单信息")
    @GetMapping("auth/getOrderInfoByOrderNo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@Parameter(name = "orderId", description = "订单id", required = true) @PathVariable String orderNo) {
        return orderService.getByOrderNo(orderNo);
    }

    @Operation(summary = "获取订单分页列表")
    @GetMapping("auth/updateOrderStatusPayed/{orderNo}/{orderStatus}")
    public Result<Void> updateOrderStatus(@PathVariable(value = "orderNo") String orderNo, @PathVariable(value = "orderStatus") Integer orderStatus) {
        orderService.updateOrderStatus(orderNo, orderStatus);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
